<!-- 商品列表 -->
<template>
  <div class="product-list">
    <!-- tab头部 -->
    <ul class="tabs" v-if="listTab">
      <li
        v-for="(tabsData, index) in tabsDatas"
        :key="tabsData.id"
        @click="changeTabs(index)"
      >
        <h4>{{ tabsData.title }}</h4>
        <span :class="activeIndex == index ? 'active' : ''">{{
          tabsData.tips
        }}</span>
      </li>
    </ul>
    <div class="loading" v-if="showloading">
      <img src="@/assets/images/loading.gif" alt="" />
    </div>
    <!-- 商品列表部分 -->
    <div class="goods-list" v-else>
      <ul>
        <li
          v-for="productData in productDatas"
          :key="productData.id"
          @click="toGoodsDetails"
        >
          <div class="goods-img">
            <img :src="productData.goods_thumb" alt="" />
          </div>
          <div class="title">{{ productData.title }}</div>
          流通价格
          <div class="currency-price">
            <span>
              <em>¥</em>
              整数部分
              <span>{{ productData.shop_price | setInt }}</span>
              小数部分
              <em>{{ productData.shop_price | setFloat }}</em>
            </span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: ["listTab"],
  data() {
    //这里存放数据
    return {
      tabsDatas: [
        {
          id: 1,
          title: "精选",
          tips: "为你推荐",
        },
        {
          id: 2,
          title: "社区",
          tips: "新奇好物",
        },
        {
          id: 3,
          title: "新品",
          tips: "潮流上新",
        },
        {
          id: 4,
          title: "热卖",
          tips: "火热爆款",
        },
      ],
      activeIndex: 0,
      productDatas: [
        {
          id: 1,
          title:
            "华帝【年度旗舰】Huawei/华为 Mate 9 6+128GB 4G智能手机限量抢 麒麟960芯片 徕卡双镜头",
          goods_thumb:
            "https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1489108266095.jpg",
          shop_price: "2333",
        },
        {
          id: 2,
          title:
            "华帝【年度旗舰】Huawei/华为 Mate 9 6+128GB 4G智能手机限量抢 麒麟960芯片 徕卡双镜头",
          goods_thumb:
            "https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1489108266095.jpg",
          shop_price: "2333",
        },
        {
          id: 3,
          title:
            "华帝【年度旗舰】Huawei/华为 Mate 9 6+128GB 4G智能手机限量抢 麒麟960芯片 徕卡双镜头",
          goods_thumb:
            "https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1489108266095.jpg",
          shop_price: "2333",
        },
        {
          id: 4,
          title:
            "华帝【年度旗舰】Huawei/华为 Mate 9 6+128GB 4G智能手机限量抢 麒麟960芯片 徕卡双镜头",
          goods_thumb:
            "https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1489108266095.jpg",
          shop_price: "2333",
        },
        {
          id: 5,
          title:
            "华帝【年度旗舰】Huawei/华为 Mate 9 6+128GB 4G智能手机限量抢 麒麟960芯片 徕卡双镜头",
          goods_thumb:
            "https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1489108266095.jpg",
          shop_price: "2333",
        },
      ],
      showloading: false,
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    changeTabs(index) {
      console.log(index);
      //   使头部tab变为活性
      this.activeIndex = index;
      this.showloading = true;
      //   数据加载完毕,loading图标消失
      setTimeout(() => {
        this.showloading = false;
      }, 1000);
    },
    toGoodsDetails() {
      this.$router.push("/goodsdetails");
    },
  },
  beforeCreate() {}, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  beforeMount() {}, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="less" scoped>
.product-list {
  width: calc(100% - 2rem);
  margin: 1rem;
  background-color: #fff;
  .tabs {
    display: flex;
    height: 4rem;
    padding: 1rem 0;
    justify-content: center;
    align-items: center;
    li {
      flex: 1;
      text-align: center;
      position: relative;
      h4 {
        font-size: 1.6rem;
        color: #333;
      }
      span {
        color: #666;
        width: 70%;
        margin-left: 10%;
        padding: 0.1rem 0.3rem;
        border-radius: 1rem;
        display: block;
      }
      .active {
        background: linear-gradient(-88deg, #ff4f2e, #f91f28);
        color: #fff;
      }
      &::after {
        content: "";
        position: absolute;
        right: 0;
        top: 10%;
        height: 80%;
        width: 0.1rem;
        background-color: #ccc;
      }
      //   伪元素的写法
      &:last-child::after {
        content: "";
        height: 0;
      }
    }
  }

  .goods-list {
    ul {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      li {
        width: 17rem;
        height: 26rem;
        background-color: #fff;
        border-radius: 1rem;
        margin: 1rem 0;
        overflow: hidden;
        img {
          width: 17rem;
          height: 17rem;
        }
        .title {
          width: 90%;
          margin: 5%;
          height: 4rem;
          line-height: 2rem;
          font-size: 1.2rem;
          // 多行文本省略号
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
        .currency-price {
          color: rgb(226, 44, 44);
          font-weight: 700;
          padding-left: 0.5rem;
          span {
            font-size: 1.5rem;
            em {
              font-size: 1rem;
            }
          }
        }
      }
    }
  }
  .loading {
    text-align: center;
    img {
      width: 50px;
    }
  }
}
</style>
